﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Mvc;

namespace MvcDownload.Controllers
{
    public class DownController : Controller
    {
        /// <summary>
        /// 普通下载
        /// </summary>
        /// <returns></returns>
        public ActionResult Normal()
        {       
            var sb = new StringBuilder("id,name\r\n");
            int i = 1;
            do
            {
                sb.Append($"{i++},abc我是谁666\r\n");
            } while (i <= 10000000);

            return File(Encoding.UTF8.GetBytes(sb.ToString()), "application/octet-stream", "normal.csv");
        }

        /// <summary>
        /// 实时下载
        /// </summary>
        /// <returns></returns>
        public ActionResult Part()
        {
            // 获取或设置一个值，该值指示是否缓冲输出并在整个响应结束处理后发送该输出
            Response.Buffer = false;
            // Response.BufferOutput = false;

            Response.AppendHeader("Content-Type", "application/octet-stream");
            Response.AppendHeader("Content-Disposition", "attachment;filename=demo.csv;filename*=UTF-8''partutf8.csv");

            string header = "id,name\r\n";
            byte[] headerBuf = System.Text.Encoding.UTF8.GetBytes(header);
            Response.OutputStream.Write(headerBuf, 0, headerBuf.Length);
            Response.OutputStream.Flush();

            int i = 1;
            do
            {
                byte[] rowBuf = System.Text.Encoding.UTF8.GetBytes($"{i++},abc我是谁666\r\n");
                Response.OutputStream.Write(rowBuf, 0, rowBuf.Length);
                Response.OutputStream.Flush();

            } while (i <= 100000);

            Response.OutputStream.Flush();
            Response.End();

            // .net core
            // Response.Body.Write(data, 0, data.Length);
            // Response.Body.Flush();
            // Response.Body.EndWrite();

            return new EmptyResult();
        }

        /// <summary>
        /// 实时下载(UTF8-BOM)
        /// </summary>
        /// <returns></returns>
        public ActionResult PartUTF8Bom()
        {
            Response.Buffer = false;
            Response.AppendHeader("Content-Type", "application/octet-stream");
            Response.AppendHeader("Content-Disposition", "attachment;filename=demo.csv;filename*=UTF-8''partutf8_bom.csv");

            // add UTF-8 BOM
            var bytes = Encoding.UTF8.GetPreamble();
            Response.OutputStream.Write(bytes, 0, bytes.Length);

            string header = "id,name\r\n";
            byte[] headerBuf = System.Text.Encoding.UTF8.GetBytes(header);
            Response.OutputStream.Write(headerBuf, 0, headerBuf.Length);
            Response.OutputStream.Flush();

            int i = 1;
            do
            {
                byte[] rowBuf = System.Text.Encoding.UTF8.GetBytes($"{i++},abc我是谁666\r\n");
                Response.OutputStream.Write(rowBuf, 0, rowBuf.Length);
                Response.OutputStream.Flush();

            } while (i <= 100);

            Response.OutputStream.Flush();
            Response.End();

            return new EmptyResult();
        }

        /// <summary>
        /// 实时下载(乱码)
        /// </summary>
        /// <returns></returns>
        public ActionResult PartWithError()
        {
            Response.Buffer = false;
            Response.AppendHeader("Content-Type", "application/octet-stream");
            Response.AppendHeader("Content-Disposition", "attachment;filename=demo.csv;filename*=UTF-8''partutf16.csv");

            string header = "id,name\r\n";
            byte[] headerBuf = System.Text.Encoding.Unicode.GetBytes(header);
            Response.OutputStream.Write(headerBuf, 0, headerBuf.Length);
            Response.OutputStream.Flush();

            int i = 1;
            do
            {
                byte[] rowBuf = System.Text.Encoding.Unicode.GetBytes($"{i++},abc我是谁666\r\n");
                Response.OutputStream.Write(rowBuf, 0, rowBuf.Length);
                Response.OutputStream.Flush();

            } while (i <= 100);

            Response.OutputStream.Flush();
            Response.End();

            return new EmptyResult();
        }

        /// <summary>
        /// 实时下载(BOM)
        /// </summary>
        /// <returns></returns>
        public ActionResult PartWithBom()
        {
            Response.Buffer = false;
            Response.AppendHeader("Content-Type", "application/octet-stream");
            Response.AppendHeader("Content-Disposition", "attachment;filename=demo.csv;filename*=UTF-8''partutf16_bom.csv");

            // add UTF-16 BOM
            var bytes = Encoding.Unicode.GetPreamble();
            Response.OutputStream.Write(bytes, 0, bytes.Length);

            string header = "id,name\r\n";
            byte[] headerBuf = System.Text.Encoding.Unicode.GetBytes(header);
            Response.OutputStream.Write(headerBuf, 0, headerBuf.Length);
            Response.OutputStream.Flush();

            int i = 1;
            do
            {
                byte[] rowBuf = System.Text.Encoding.Unicode.GetBytes($"{i++},abc我是谁666\r\n");
                Response.OutputStream.Write(rowBuf, 0, rowBuf.Length);
                Response.OutputStream.Flush();

            } while (i <= 100);

            Response.OutputStream.Flush();
            Response.End();

            return new EmptyResult();
        }
    }
}